<?php
namespace app\index\controller;
use think\Controller;

class Detail extends Base
{
    public function index($id)
    {
        if(!intval($id)){
            $this->error('id不合法');
        }
        //查询id商品
        $deal = model('Deal')->get($id);
        if(!$deal || $deal->status != 1 ){
            $this->error('商品不存在');
        }
        //获取分类信息
       $category = model('Category')->get($deal->category_id);
        //获取分店信息
        $locations = model('BisLocation')->getNormalLocationsInId($deal->location_ids);

        $flag = 0;
        if($deal->start_time > time() ){
            $flag = 1;
            $sub = $deal->start_time - time();
            $timedata = '';
            $d = floor($sub/(3600*24));
            if($d){
                $timedata .= $d . "天";
            }
            $h = floor($sub%(3600*24)/3600);
            if($h){
                $timedata .= $h . "时";
            }
            $m = floor($sub%(3600*24)%3600/60);
            if($m){
                $timedata .= $m . "分";
            }
            $s = floor($sub%(3600*24*60)%60);
            if($s){
                $timedata .= $s . "秒";
            }

            $this->assign('timedata',$timedata);


        }
        return $this->fetch('',[
            'title' => $deal->name,
            'category' => $category,
            'locations' => $locations,
            'deal' => $deal,
            'overplus' => $deal->total_count - $deal->buy_count,
            'flag' =>$flag,
            'mapstr' =>$locations[0]['xpoint'].','.$locations[0]['ypoint']
        ]);
    }
}
